import javax.swing.*;

import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.HeadlessException;
import java.awt.event.*;
import java.util.Observable;
import java.util.Observer;

/**
 * 
 * the graphical user interface that displays when program is run
 *
 */
public class MVCRestaurantView extends JFrame  implements Observer {

	MenuList menuLister;
	Orders ordersLister;
	KitchenToHatch kth;
	HatchToTable htt;
	int tableNo;
	
	
	
	
	JPanel frame = new JPanel();
	JPanel kitchenPane = new JPanel();
	JPanel hatchPane = new JPanel();
	JPanel tablesPane = new JPanel();
	JPanel bluePane = new JPanel();
	JPanel greenPane = new JPanel();
	JPanel redPane = new JPanel();
	JPanel bt1Pane = new JPanel();
	JPanel bt2Pane = new JPanel();
	JPanel gt1Pane = new JPanel();
	JPanel gt2Pane = new JPanel();
	JPanel rt1Pane = new JPanel();
	JPanel rt2Pane = new JPanel();
	JPanel buttons = new JPanel();
	
	JButton bt1Button = new JButton("Bill");
	JButton bt2Button = new JButton("Bill");
	JButton gt1Button = new JButton("Bill");
	JButton gt2Button = new JButton("Bill");
	JButton rt1Button = new JButton("Bill");
	JButton rt2Button = new JButton("Bill");
	JButton start = new JButton("Start");
	JButton stop = new JButton("Stop Restaurant");
	
	JTextArea kitchenArea= new JTextArea(15,25);
	JTextArea hatchArea= new JTextArea(15,25);
	JTextArea bt1Area= new JTextArea(10,27);
	JTextArea bt2Area= new JTextArea(10,27);
	JTextArea gt1Area= new JTextArea(10,27);
	JTextArea gt2Area= new JTextArea(10,27);
	JTextArea rt1Area= new JTextArea(10,27);
	JTextArea rt2Area= new JTextArea(10,27);
	
	JTabbedPane kitchenTab = new JTabbedPane();	// Creating object of KITCHEN tab
	JTabbedPane hatchTab = new JTabbedPane();	// Creating object of KITCHEN tab
	JTabbedPane blueTab = new JTabbedPane();	// Creating object of BLUE tab  
	JTabbedPane greenTab = new JTabbedPane();	// Creating object of GREEN tab  
	JTabbedPane redTab = new JTabbedPane();	// Creating object of RED tab
	JTabbedPane bt1Tab = new JTabbedPane();	// Creating object of BLUE table 1 tab  
	JTabbedPane bt2Tab = new JTabbedPane();	// Creating object of BLUE table 2 tab  
	JTabbedPane gt1Tab = new JTabbedPane();	// Creating object of GREEN table 1 tab  
	JTabbedPane gt2Tab = new JTabbedPane();	// Creating object of GREEN table 2 tab
	JTabbedPane rt1Tab = new JTabbedPane();	// Creating object of RED table 1 tab	  
	JTabbedPane rt2Tab = new JTabbedPane();	// Creating object of RED table 2 tab
	JFrame f;
	
	/**
	 * Constructor that initializes menulist and orders list
	 * menuList
	 * o orders
	 */
	public MVCRestaurantView(MenuList menuList, Orders orderList, KitchenToHatch kt, HatchToTable ht)
	{
		f = new JFrame();
		ordersLister = orderList;
		ordersLister.addObserver(this);
		kth = kt;
		htt = ht;
		kth.addObserver(this);
		htt.addObserver(this);
		
		menuLister = menuList;
		setLocation(80,80);
		f.setTitle("Menu");	// Naming title of the Frame
		setSize(650,650); 	// Making a readable size
		setVisible(true);	// Making the Frame visible
		
	/** GUI for the menu **/
		JTabbedPane menus = new JTabbedPane();	// Creating object of tabbed pane 
		f.add(menus);
		getContentPane().add(menus);	// Sets the tabbed panes in the content frame	
		
		JPanel arab = new JPanel();	// Creates the Arabic cuisine tab
		JPanel chinese = new JPanel();	// Creates the Chinese cuisine tab
		JPanel indian = new JPanel();	// Creates the Indian cuisine tab
		JPanel italian = new JPanel();	// Creates the Italian cuisine tab
		JPanel mexican = new JPanel();	// Creates the Mexican cuisine tab
		
		JTextArea arabicMenu = new JTextArea();	// Creating non editable Text Area for menu and putting in first tab
		arabicMenu.setEditable(false);
		arabicMenu.setFont(new Font (Font.MONOSPACED, Font.PLAIN,14));
		arabicMenu.setText(menuLister.getArabicCuisine());		
		arab.add(arabicMenu);
		
		
		JTextArea chineseMenu = new JTextArea();	// Creating non editable Text Area for menu and putting in first tab
		chineseMenu.setEditable(false);
		chineseMenu.setFont(new Font (Font.MONOSPACED, Font.PLAIN,14));
		chineseMenu.setText(menuLister.getChineseCuisine());
		chinese.add(chineseMenu);

		
		JTextArea indianMenu = new JTextArea();	// Creating non editable Text Area for menu and putting in first tab
		indianMenu.setEditable(false);
		indianMenu.setFont(new Font (Font.MONOSPACED, Font.PLAIN,14));
		indianMenu.setText(menuLister.getIndianCuisine());
		indian.add(indianMenu);
		
		JTextArea italianMenu = new JTextArea();	// Creating non editable Text Area for menu and putting in first tab
		italianMenu.setEditable(false);
		italianMenu.setFont(new Font (Font.MONOSPACED, Font.PLAIN,14));
		italianMenu.setText(menuLister.getItalianCuisine());
		italian.add(italianMenu);
		
		JTextArea mexicanMenu = new JTextArea();	// Creating non editable Text Area for menu and putting in first tab
		mexicanMenu.setEditable(false);
		mexicanMenu.setFont(new Font (Font.MONOSPACED, Font.PLAIN,14));
		mexicanMenu.setText(menuLister.getMexicanCuisine());
		mexican.add(mexicanMenu);
	
		menus.addTab("Arabic Cuisine", arab);	// Adding the tabs to the Frame
		menus.addTab("Chinese Cuisine", chinese);
		menus.addTab("Indian Cuisine", indian);
		menus.addTab("Italian Cuisine", italian);
		menus.addTab("Mexican Cuisine", mexican);
		setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
	
		/** This is to call the table bill function when the current frame closes **/
		addWindowListener( new WindowAdapter() 
		{
            public void windowClosing(WindowEvent we) 
            {
            	setVisible(false);
            	TableBill();
            	f.dispose();
            }
		});
	}
	
	
	public void TableBill(){
		frame.setLocation(80,40);
		setTitle("Restaurant");	// Naming title of the Frame
		frame.setSize(550,470); 	// Making a readable size
		
		frame.setLayout(new FlowLayout());
		bluePane.setLayout(new BoxLayout(bluePane, BoxLayout.PAGE_AXIS));
		greenPane.setLayout(new BoxLayout(greenPane, BoxLayout.PAGE_AXIS));
		redPane.setLayout(new BoxLayout(redPane, BoxLayout.PAGE_AXIS));
		kitchenPane.setLayout(new BoxLayout(kitchenPane, BoxLayout.PAGE_AXIS));
		buttons.setLayout(new BoxLayout(buttons, BoxLayout.LINE_AXIS));
		
		kitchenArea.setText("Welcome to the Kitchen");
		hatchArea.setText("Hello Hatch");
		bt1Area.setText("Table Blue1");
		bt2Area.setText("Table Blue2");
		gt1Area.setText("Table Green1");
		gt2Area.setText("Table Green2");
		rt1Area.setText("Table Red1");		
		rt2Area.setText("Table Red2");
		
		JScrollPane j = new JScrollPane(kitchenArea);
		
		kitchenArea.setEditable(false);
		hatchArea.setEditable(false);
		bt1Area.setEditable(false);
		bt2Area.setEditable(false);
		gt1Area.setEditable(false);
		gt2Area.setEditable(false);
		rt1Area.setEditable(false);		
		rt2Area.setEditable(false);

		kitchenArea.setVisible(true);
		hatchArea.setVisible(true);
		bt1Area.setVisible(true);
		bt2Area.setVisible(true);
		gt1Area.setVisible(true);
		gt2Area.setVisible(true);
		rt1Area.setVisible(true);		
		rt2Area.setVisible(true);
		
		buttons.add(start);
		buttons.add(stop);
		kitchenPane.add(j);
		kitchenPane.add(buttons);
		hatchPane.add(hatchArea);
		kitchenPane.add(hatchTab);
		bt1Pane.add(bt1Area);
		bt2Pane.add(bt2Area);
		gt1Pane.add(gt1Area);
		gt2Pane.add(gt2Area);
		rt1Pane.add(rt1Area);
		rt2Pane.add(rt2Area);
		bluePane.add(bt1Tab);
		bluePane.add(bt1Button);
		bluePane.add(bt2Tab);
		bluePane.add(bt2Button);
		greenPane.add(gt1Tab);
		greenPane.add(gt1Button);
		greenPane.add(gt2Tab);
		greenPane.add(gt2Button);
		redPane.add(rt1Tab);
		redPane.add(rt1Button);
		redPane.add(rt2Tab);
		redPane.add(rt2Button);
		
		
		kitchenTab.addTab("Kitchen",kitchenPane);
		hatchTab.addTab("Hatch",hatchPane);
		bt1Tab.addTab("Blue Table 1",bt1Pane);
		bt2Tab.addTab("Blue Table 2",bt2Pane);
		gt1Tab.addTab("Green Table 1",gt1Pane);
		gt2Tab.addTab("Green Table 2",gt2Pane);
		rt1Tab.addTab("Red Table 1",rt1Pane);		
		rt2Tab.addTab("Red Table 2",rt2Pane);
		blueTab.addTab("Blue Tables",bluePane);
		greenTab.addTab("Green Tables",greenPane);
		redTab.addTab("Red Tables",redPane);
		
		JScrollPane big = new JScrollPane(frame);
		setContentPane(big);
		frame.add(kitchenTab);
		frame.add(blueTab);
		frame.add(greenTab);
		frame.add(redTab);
		stop.setEnabled(false);

		
		pack();
		setVisible(true);
		frame.setVisible(true);
		addWindowListener( new WindowAdapter() 
		{
            public void windowClosing(WindowEvent we) 
            {
            	setVisible(false);

            }
		});
	}

	 public void addbt1Listener(ActionListener al) {
	        bt1Button.addActionListener(al);
	 }
	 
	 public void addbt2Listener(ActionListener al) {
	        bt2Button.addActionListener(al);
	 }
	 
	 public void addgt1Listener(ActionListener al) {
	        gt1Button.addActionListener(al);
	 }
	 
	 public void addgt2Listener(ActionListener al) {
	        gt2Button.addActionListener(al);
	 }
	 
	 public void addrt1Listener(ActionListener al) {
	        rt1Button.addActionListener(al);
	 }
	 
	 public void addrt2Listener(ActionListener al) {
	        rt2Button.addActionListener(al);
	 }
	 
	 public void addstartListener(ActionListener al) {
	        start.addActionListener(al);
	 }
	 
	 public void addstopListener(ActionListener al) {
	        stop.addActionListener(al);
	 }
	 
	 public void disableStart() {
	        start.setEnabled(false);
	        stop.setEnabled(true);
	    }
	
	@Override
	public void update(Observable o, Object arg) {
		kitchenArea.setText(ordersLister.getKitchenOrder());
		hatchArea.setText(kth.displayHatch());
		bt1Area.setText(htt.getBt1());
		bt2Area.setText(htt.getBt2());
		gt1Area.setText(htt.getGt1());
		gt2Area.setText(htt.getGt2());
		rt1Area.setText(htt.getRt1());		
		rt2Area.setText(htt.getRt2());
		
	}
	
	
	
	/**
	 * Pops up a JOptionpane where user selects the table and its bill is shown
	 */
}